package cn.yanxiaotong.Mygame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;

import cn.yanxiaotong.MyFrame.Constant;
import cn.yanxiaotong.MyFrame.MyFrame;
import cn.yanxiaotong.Mygame.Tank.Direction;

public class Missile {
	private int x, y;
	private int missileW=Constant.MissileW;
	private int missileH=Constant.MissileH;
	private int Mspeed = Constant.MSPEED;
	private boolean live = true;
	private boolean good = true;
	 MyFrame mf;
	Tank.Direction dir;

	public Missile(int x, int y, Tank.Direction dir, boolean good ,MyFrame mf) {
		this.x = x;
		this.y = y;
		 this.mf=mf;
		this.dir = dir;
		this.good = good;
	}

	public void draw(Graphics g) {
		if (live == true) {
			Color c = g.getColor();
		if(good)
			g.setColor(Color.orange);
		else 
			g.setColor(Color.BLACK);
			g.fillOval(x, y, missileW, missileH);
			g.setColor(c);
			fly();
		}
	}
	public boolean hitWall(greatWall gw){
		if(this.isLive()&&this.getRect().intersects(gw.getRect())){
			this.setLive(false);
			Explode ex = new Explode(x,y,mf);
			mf.bongs.add(ex);
			return true;
		}
		return false;
	}
	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	public void fly() {
		switch (dir) {
		case L:
			x -= Constant.MSPEED;
			break;
		case LU:
			x -= Constant.MSPEED;
			y -= Constant.MSPEED;
			break;
		case LD:
			x -= Constant.MSPEED;
			y += Constant.MSPEED;
			break;
		case R:
			x += Constant.MSPEED;
			break;
		case RU:
			x += Constant.MSPEED;
			y -= Constant.MSPEED;
			break;
		case RD:
			x += Constant.MSPEED;
			y += Constant.MSPEED;
			break;
		case U:
			y -= Constant.MSPEED;
			break;
		case D:
			y += Constant.MSPEED;
			break;
		case STOP:
			break;
		}
		if (x <= 25 || x >= Constant.FWID - Constant.MissileW || y <= 0
				|| y >= Constant.FHEI - Constant.MissileH) {
			live = false;
			mf.missiles.remove(this);
		}
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Rectangle getRect() {
		Rectangle r = new Rectangle(x, y, Constant.MissileW, Constant.MissileH);
		return r;
	}

	public boolean destroy(Tank t1) {
		if (this.isLive() && this.getRect().intersects(t1.getRect())
				&& t1.isLive() && this.good != t1.isGood()) {
			Explode ex = new Explode(x,y,mf);
			mf.bongs.add(ex);
			if(t1.isGood()){
				this.setLive(false);
				if(t1.getLife()>0){
				t1.setLife(t1.getLife()-20);
				}
				if(t1.getLife()==0){
					t1.setLive(false);
				}
			}else{
			t1.setLive(false);
			this.setLive(false);
			}
			return true;
		}
		return false;
	}

	public boolean destroy(List<Tank> enemyTanks) {
		for (int i = 0; i < enemyTanks.size(); i++) {
			Tank enemyTank = enemyTanks.get(i);
			if (destroy(enemyTank)) {
				enemyTank.setLive(false);
				enemyTanks.remove(i);
				return true;
			}
		}
		return false;
	}
}
